package com.example.arithmeticleetcode.learnArithmetic2.dynamic_programming;

/**
 * @program: arithmetic-leetcode
 * @description: 最长公共子序列
 * @author: FangZhen
 * @create: 2020-12-21 13:11
 **/
public class LCS {

    public static void main(String[] args) {
        System.out.println(lcs(new int[]{1, 3, 5, 9, 10}, new int[]{1, 4, 9, 10, 8, 9}));
        System.out.println(lcs1(new int[]{1, 3, 5, 9, 10}, new int[]{1, 4, 9, 10, 8, 9}));
        System.out.println(lcs2(new int[]{1, 3, 5, 9, 10}, new int[]{1, 4, 9, 10, 8, 9}));
        System.out.println(lcs3(new int[]{1, 3, 5, 9, 10}, new int[]{1, 4, 9, 10, 8, 9}));
        System.out.println(lcs4(new int[]{1, 3, 5, 9, 10}, new int[]{1, 4, 9, 10, 8, 9}));

        System.out.println("--------------------------");
        System.out.println(lcs(new int[]{1, 4, 5, 9, 10}, new int[]{1, 4, 9, 10, 8, 9}));
        System.out.println(lcs1(new int[]{1, 4, 5, 9, 10}, new int[]{1, 4, 9, 10, 8, 9}));
        System.out.println(lcs2(new int[]{1, 4, 5, 9, 10}, new int[]{1, 4, 9, 10, 8, 9}));
        System.out.println(lcs3(new int[]{1, 4, 5, 9, 10}, new int[]{1, 4, 9, 10, 8, 9}));
        System.out.println(lcs4(new int[]{1, 4, 5, 9, 10}, new int[]{1, 4, 9, 10, 8, 9}));
    }

    /**
     * 最长公共子序列
     *
     * @param text1
     * @param text2
     * @return
     */
    public int longestCommonSubsequence(String text1, String text2) {
        if (text1 == null || text2 == null) return 0;
        char[] chars1 = text1.toCharArray();
        if (chars1.length == 0) return 0;
        char[] chars2 = text2.toCharArray();
        if (chars2.length == 0) return 0;
        char[] rowsNums = chars1, closNums = chars2;
        if (chars1.length < chars2.length) {
            rowsNums = chars2;
            closNums = chars1;
        }
        int[] dp = new int[closNums.length + 1];
        for (int i = 1; i <= rowsNums.length; i++) {
            int cur = 0;
            for (int j = 1; j <= closNums.length; j++) {
                int leftTop = cur;
                cur = dp[j];
                if (rowsNums[i - 1] == closNums[j - 1]) {
                    dp[j] = leftTop + 1;
                } else {
                    dp[j] = Math.max(dp[j - 1], dp[j]);
                }
            }
        }
        return dp[closNums.length];
    }

    public static int lcs4(int[] nums1, int[] nums2) {
        if (nums1 == null || nums1.length == 0) return 0;
        if (nums2 == null || nums2.length == 0) return 0;
        int[] rowsNums = nums1, closNums = nums2;
        if (nums1.length < nums2.length) {
            rowsNums = nums2;
            closNums = nums1;
        }
        int[] dp = new int[closNums.length + 1];
        for (int i = 1; i <= rowsNums.length; i++) {
            int cur = 0;
            for (int j = 1; j <= closNums.length; j++) {
                int leftTop = cur;
                cur = dp[j];
                if (rowsNums[i - 1] == closNums[j - 1]) {
                    dp[j] = leftTop + 1;
                } else {
                    dp[j] = Math.max(dp[j - 1], dp[j]);
                }
            }
        }
        return dp[closNums.length];
    }

    public static int lcs3(int[] nums1, int[] nums2) {
        if (nums1 == null || nums1.length == 0) return 0;
        if (nums2 == null || nums2.length == 0) return 0;
        int[] dp = new int[nums2.length + 1];
        for (int i = 1; i <= nums1.length; i++) {
            int cur = 0;
            for (int j = 1; j <= nums2.length; j++) {
                int leftTop = cur;
                cur = dp[j];
                if (nums1[i - 1] == nums2[j - 1]) {
                    dp[j] = leftTop + 1;
                } else {
                    dp[j] = Math.max(dp[j - 1], dp[j]);
                }
            }
        }
        return dp[nums2.length];
    }

    public static int lcs2(int[] nums1, int[] nums2) {
        if (nums1 == null || nums1.length == 0) return 0;
        if (nums2 == null || nums2.length == 0) return 0;
        int[][] dp = new int[2][nums2.length + 1];
        for (int i = 1; i <= nums1.length; i++) {
            int row = i & 1;
            int preRow = (i - 1) & 1;
            for (int j = 1; j <= nums2.length; j++) {
                if (nums1[i - 1] == nums2[j - 1]) {
                    dp[row][j] = dp[preRow][j - 1] + 1;
                } else {
                    dp[row][j] = Math.max(dp[row][j - 1], dp[preRow][j]);
                }
            }
        }
        return dp[nums1.length & 1][nums2.length];
    }

    public static int lcs1(int[] nums1, int[] nums2) {
        if (nums1 == null || nums1.length == 0) return 0;
        if (nums2 == null || nums2.length == 0) return 0;
        int[][] dp = new int[nums1.length + 1][nums2.length + 1];
        for (int i = 1; i <= nums1.length; i++) {
            for (int j = 1; j <= nums2.length; j++) {
                if (nums1[i - 1] == nums2[j - 1]) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j]);
                }
            }
        }
        return dp[nums1.length][nums2.length];
    }


    /**
     * 递归实现 空间复杂度O(n) n为两个数组最小的长度 时间复杂度O(n^2)
     *
     * @param nums1
     * @param nums2
     * @return
     */
    public static int lcs(int[] nums1, int[] nums2) {
        if (nums1 == null || nums1.length == 0) return 0;
        if (nums2 == null || nums2.length == 0) return 0;
        return lcs(nums1, nums1.length, nums2, nums2.length);
    }

    private static int lcs(int[] nums1, int i, int[] nums2, int j) {
        if (i == 0 || j == 0) return 0;
        if (nums1[i - 1] == nums2[j - 1]) return lcs(nums1, i - 1, nums2, j - 1) + 1;
        return Math.max(lcs(nums1, i - 1, nums2, j),
                lcs(nums1, i, nums2, j - 1));
    }
}
